package com.zhenyi.liuxue.admin;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

import com.cenjiawen.common.IndexController;
import com.cjw.model.DBMap;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.liuxue.common.model.LxMajor;
import com.liuxue.common.model.LxSchool;
import com.liuxue.common.model.LxSchoolMajor;
import com.liuxue.common.model.LxSchoolRank;
import com.liuxue.common.model.LxUkSchool;
import com.utils.PathUtils;
import com.utils.image.UploadUtils;
import com.zhenyi.liuxue.core.ConvertUtils;
import com.zhenyi.liuxue.core.ImportUtils;
import com.zhenyi.liuxue.interceptor.AuthInterceptor;

import cn.hutool.core.io.FileUtil;

@Before(AuthInterceptor.class)
public class ApiController extends IndexController {

	public void uploadFile() {
		// String path
		File file = getFileParam("file");
		File path = new File(PathUtils.getFile("lx", "attachement", file.getName()));
		FileUtil.move(file, path, true);
		StringBuilder sb = new StringBuilder();
		sb.append("/lx/admin/api/file?name=").append(file.getName());

		renderResult(sb.toString());
	}

	// @Clear
	// public void img() {
	// new ConvertUtils().run();
	// renderSuccess();
	// }

	public void file() {
		String name = getPara("name");
		if (StrKit.isBlank(name)) {
			renderFailure();
			return;
		}
		// File file = new File(PathKit.getWebRootPath() + File.separatorChar +
		// "upload" + File.separatorChar + name);
		File file = new File(PathUtils.getFile("lx", "attachement", name));
		if (!file.exists()) {
			renderFailure(404, "not found");
			return;
		}
		renderFile(file);
	}

	public void uploadImage() {
		File file = getFileParam("file");
		String name = file.getName();
		String url = UploadUtils.upload(file);
		DBMap map = new DBMap();
		map.put("name", name);
		map.put("url", url);
		renderSuccess(map);
	}
	
	/**
	 * 批量导入
	 * 
	 * @param file
	 * @param type 0:大学排名 1:录取标准 2:课程信息 3:公司列表
	 * @param ukSchoolId 用于导入大学排名和录取标准
	 * @param schoolId 用于导入课程信息
	 */
	public void uploadExcel() {
		File file = getFileParam("file");
		Integer type = getParaToInt("type");
		Long ukSchoolId = getParaToLong("ukSchoolId");
		if (file == null) {
			renderFailure(500, "请选择文件");
			return;
		}
		if (type == null) {
			renderFailure();
			return;
		}
		if (type <= 2 && ukSchoolId == null) {
			renderFailure();
			return;
		}
		
		boolean flag = false;
		ImportUtils utils = new ImportUtils();
		if (type.equals(0)) { // 大学排名
			flag = Db.tx(new IAtom() {
				@Override
				public boolean run() throws SQLException {
					List<LxSchoolRank> list = LxSchoolRank.dao.search("ukSchoolId", ukSchoolId);
					for (LxSchoolRank lxSchoolRank : list) {
						lxSchoolRank.delete();
					}
					return utils.importSchoolRanking(file, ukSchoolId);
				}
			});
			
		} else if (type.equals(1)) { // 录取标准
			flag = Db.tx(new IAtom() {
				@Override
				public boolean run() throws SQLException {
					List<LxSchoolMajor> list = LxSchoolMajor.dao.search("schoolId", ukSchoolId);
					for (LxSchoolMajor lxSchoolMajor : list) {
						lxSchoolMajor.delete();
					}
					return utils.importSchoolMajor(file, ukSchoolId);
				}
			});
			
		} else if (type.equals(2)) { // 课程信息
			LxUkSchool ukSchool = LxUkSchool.dao.findById(ukSchoolId);
			if (ukSchool == null) {
				renderFailure();
				return;
			}
			LxSchool school = LxSchool.dao.searchFirst("unionSchoolId", ukSchool.getUnionSchoolId());
			
			flag = Db.tx(new IAtom() {
				@Override
				public boolean run() throws SQLException {
					List<LxMajor> list = LxMajor.dao.search("schoolId", school.getId());
					for (LxMajor lxMajor : list) {
						lxMajor.delete();
					}
					return utils.importMajors(file, school.getId());
				}
			});
			
		} else if (type.equals(3)) {
			flag = Db.tx(new IAtom() {
				@Override
				public boolean run() throws SQLException {
					return utils.importCompany(file);
				}
			});
			
		} else {
			renderFailure();
			return;
		}
		
		renderFlag(flag);
	}

}
